<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="domain.Product" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="dao.ProductDao" %>
<%@ page import="java.lang.String" %>
<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/24
  Time: 10:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<html>
<head>
    <title>我的购物车</title>
</head>
<body>
<script>
    // 当商品数量发生变化时触发该方法
    function changeProductNum(count, id) {
        count = parseInt(count);
        // 如果数量为0
        if (count === 0) {
            alert("不能再减少了！");
            count = 1;
        }
        location.href = "${pageContext.request.contextPath}/cart.jsp?id=" + id + "&count=" + count;
    }
    // 删除购物车中的商品
    function cart_del() {
        let msg = "您确定要删除该商品吗？";
        return confirm(msg) === true;
    }
</script>
<%@include file="head.jsp"%>
<%
    Map<Product,Integer> cart = (Map<Product, Integer>) session.getAttribute("cart");
    String alterString = request.getParameter("id");
    String countString = request.getParameter("count");
    ProductDao dao = new ProductDao();
    if (alterString!=null&&countString!=null){
        try {
            int alter = Integer.parseInt(alterString);
            int count = Integer.parseInt(countString);
            Product p = dao.searchInMap(cart, alter);
            // 修改购物车中指定的商品数量，如果count为0，表示删除该商品
            if (count!=0){
                cart.put(p,count);
            }
            else{
                cart.remove(p);
            }
            session.setAttribute("cart",cart);
            response.sendRedirect(request.getContextPath()+"/cart.jsp");
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
    }
    if (cart==null||cart.size()==0){
        if (cart==null){
            cart = new HashMap<>();
            session.setAttribute("cart",cart);
        }
        out.print("<div style=\"text-align: center;\">您的购物车空空如也，快去<a href=\"${pageContext.request.contextPath}/main.jsp\"" +
                " style=\"color: blue;font-weight: bold\">主页</a>选购商品加购吧！</div>");
    }else{
%>
<br /><br />
<h2 style="text-align: center;color: darkslategray"><%=user.getUsername()%>的购物车</h2>
<br /><br />
<table style="border: 2px lightskyblue solid;width: 70%;margin-left: 15%">
    <tr>
        <td style="width: 10%">序号</td>
        <td style="width: 30%">名称</td>
        <td style="width: 10%">价格</td>
        <td style="width: 20%;text-align: center">数量</td>
        <td style="width: 10%">库存</td>
        <td style="width: 10%">小计</td>
        <td style="width: 10%">移除</td>
    </tr>
</table>
<%-- 循坏输出商品信息 --%>
<c:set var="total" value="0" />
<c:forEach items="${cart}" var="entry" varStatus="vs">
    <table style="width: 70%;margin-left: 15%">
        <tr>
            <td style="width: 10%;">${vs.count}</td>
            <td style="width: 30%;">${entry.key.name }</td>
            <td style="width: 10%;">${String.format("%.2f", entry.key.price)}</td>
            <td style="width: 20%;text-align: center">
                <%-- 变更商品数量 --%>
                <input type="button" value='-' style="width:20px"
                       onclick="changeProductNum('${entry.value-1}','${entry.key.id}')">
                <%-- 商品数量显示 --%>
                <label>
                    <input name="text" type="text" value="${entry.value}" style="width:40px;text-align:center"/>
                </label>
                <input type="button" value='+' style="width:20px"
                       onclick="changeProductNum('${entry.value+1}','${entry.key.id}')">
            </td>
            <td style="width: 10%;">${entry.key.count}</td>
            <td style="width: 10%;">${String.format("%.2f", entry.key.price*entry.value)}</td>
            <td style="width: 10%;">
                <!-- 删除商品 -->
                <a href="${pageContext.request.contextPath}/cart.jsp?id=${entry.key.id}&count=0"
                   style="color: red; font-weight:bold" onclick="return cart_del()">移除</a>
            </td>
        </tr>
    </table>
    <c:set value="${total+entry.key.price*entry.value}" var="total" />
</c:forEach>
<table style="border: 2px lightskyblue solid;width: 70%;margin-left: 15%">
    <tr>
        <td style="text-align: right;padding-right: 40px">
            <div style="color: orange;font-weight: bold">合计：&nbsp;&nbsp;￥${String.format("%.2f", total)}</div>
        </td>
    </tr>
</table>
<div style="text-align: right;margin-top: 10px; width: 70%;margin-left: 15%">
    <a href="${pageContext.request.contextPath}/main.jsp" style="color: green;font-size: large;font-weight: bold">继续购物</a>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}/checkout.jsp" style="color: purple;font-size: large;font-weight: bold">结算</a>
</div>
<%
    }
%>
<%@include file="tail.jsp"%>
</body>
</html>
